package xyz.robinbingo.pfboot.config.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import xyz.robinbingo.pfboot.constants.SecurityConstants;
import xyz.robinbingo.pfboot.security.CustomUserDetailsServiceImpl;

/**
 * WebSecurityConfig
 *
 * @author Robin
 * @date 2018/10/23 14:22
 **/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private CustomUserDetailsServiceImpl customUserDetailsService;

    @Override
    protected void configure(HttpSecurity http) {
        try {
            http.csrf().disable();
            http
                    .userDetailsService(customUserDetailsService)
                    .authorizeRequests()
                    .antMatchers(SecurityConstants.PAGE_PERMIT_ALL).permitAll()
                    .antMatchers(SecurityConstants.PAGE_ROLL_ADMIN).access("hasRole('ROLE_ADMIN')")
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    .loginPage("/login-default.xhtml")
                    .permitAll()
                    .failureUrl("/login-default.xhtml?error=true")
                    .defaultSuccessUrl("/index.xhtml")
                    .and()
                    .logout()
                    .logoutSuccessUrl("/login-default.xhtml");
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}